package dao.impl;


import dao.ProductDao;
import domain.Product;
import util.JDBCUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

public class ProductDaoImpl implements ProductDao {
    @Override
    public List<Product> searchAll(){
        String sql="select * from products";
        List<Product> ps = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtil.getConnection();
            statement = connection.prepareStatement(sql);
            resultSet = statement.executeQuery();
            Product product;
            while ((product = JDBCUtil.parseResultSet(resultSet, Product.class)) != null) {
                ps.add(product);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeAll(connection, statement, resultSet);
        }
        return ps;
    }

    @Override
    public List<Product> search(String name) {
        String sql="select * from products where name like ?";
        List<Product> ps = new ArrayList<>();
        Connection connection = null;
        PreparedStatement statement = null;
        ResultSet resultSet = null;
        try {
            connection = JDBCUtil.getConnection();
            statement = connection.prepareStatement(sql);
            statement.setString(1, "%" + name + "%");
            resultSet = statement.executeQuery();
            Product product;
            while ((product = JDBCUtil.parseResultSet(resultSet, Product.class)) != null) {
                ps.add(product);
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeAll(connection, statement, resultSet);
        }
        return ps;
    }

    @Override
    public boolean add(Product product) {
        String sql = "insert into products(id, name, price, category, pnum, imgurl, description) values(?, ?, ?, ?, ?, ?, ?)";
        Connection connection = null;
        PreparedStatement statement = null;
        int result = 0;
        try {
            connection = JDBCUtil.getConnection();
            statement = connection.prepareStatement(sql);
            statement.setString(1, UUID.randomUUID().toString());
            statement.setString(2, product.getName());
            statement.setDouble(3, product.getPrice());
            statement.setString(4, product.getCategory());
            statement.setInt(5, product.getPnum());
            statement.setString(6, product.getImgurl());
            statement.setString(7, product.getDescription());
            result = statement.executeUpdate();
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            JDBCUtil.closeAll(connection, statement, null);
        }
        return result != 0;
    }
}
